﻿using ControlHelper.WPF;
using Creator.Core;
using Creator.Core.Base;
using Creator.Core.Component;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DesktopCreator.Controls
{
    /// <summary>
    /// UCAction.xaml 的交互逻辑
    /// </summary>
    public partial class UCAction : UserControl
    {
        public UCAction()
        {
            InitializeComponent();
            txtTitle.MouseDown += (s, e) =>
            {
                gridMain.Visibility = gridMain.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
            };
            //点击移除这个过程
            txtRemove.MouseDown += (s, e) =>
            {
                var dialog = MessageBox.Show("是否确认删除这个过程", "注意", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (dialog != MessageBoxResult.Yes) return;

                if (Action == null) return;
                //在这个对象的行为里面找到所有是 行为集合的进行移除
                foreach (var behaviour in Action.BaseObject.Behaviours)
                {
                    if (behaviour is ActionList alist)
                    {
                        var ac = alist.Actions.Where(a => a.Id == Action.Id).FirstOrDefault();
                        if (ac != null)
                        {
                            alist.RemoveAction(ac);
                            OnRemoved?.Invoke(this);
                        }
                    }
                }
            };
        }

        public UCAction(IAction action):this()
        {
            Title = action.Name;
            Action = action;

            var cl = new ClassControl(action.GetType(),true, action,values:action.BaseObject);

            gridMain.Children.Add(cl);  
        }
        public event Action<FrameworkElement> OnRemoved;
        public IAction Action { get; private set; }


        public string Title
        {
            get => txtTitle.Text; private set
            {
                txtTitle.Text = value;
            }
        }
    }
}
